package com.cubomagicogoias.site;

import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cubomagicogoias.model.Pessoa;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * 
 * @author Jose Fernando Tolentino
 */
public class Sessao implements Serializable {

	public static final Logger log = Logger.getLogger("Tolentino");
	/**
	 * 
	 */
	private static final long serialVersionUID = 8780250502974677126L;

	public String loginName() {
		return get("loginName");
	}

	public String loginEmail() {
		return get("loginEmail");
	}

	public String loginRedirect() {
		return get("loginRedirect");
	}

	private transient HttpSession ss;

	public static Sessao init(HttpServletRequest req) {
		HttpSession ss = req.getSession(true);
		Sessao s = (Sessao) ss.getAttribute("sessao");
		if (s == null) {
			s = new Sessao();
			ss.setAttribute("sessao", s);
		}
		s.ss = ss;
		return s;
	}

	public boolean login(DAO dao, String email, String password) {
		return login(dao, email, password.hashCode());
	}

	public boolean login(DAO dao, String email, int password) {
		set("loginOk", null);
		set("loginName", "");
		set("loginEmail", "");
		Pessoa u = dao.procuraPessoa(email);
		if (u == null)
			return false;
		if (u.getSenha() != password)
			return false;

		set("loginName", u.getNome());
		set("loginEmail", u.getEmail());
		set("loginOk", "T");
		return true;
	}

	public boolean logged() {
		return "T".equals(get("loginOk"));
	}

	public void logout() {
		set("loginRedirect", null);
		set("loginOk", null);
		set("loginName", "");
		set("loginEmail", "");
		ss.invalidate();
	}

	public void set(String item, String value) {
		ss.setAttribute("v_" + item, value);
	}

	public String get(String item) {
		String s = (String) ss.getAttribute("v_" + item);
		if (s == null)
			return "";
		return s;
	}

	public String id() {
		return ss.getId();
	}

	public void setLastErrorMsg(String lastErrorMsg) {
		set("_lastErrorMsg", lastErrorMsg);
	}

	public String showError() {
		String s = get("_lastErrorMsg");
		if (s == null || "".equals(s))
			return "";
		setLastErrorMsg(null);
		String r = "<p class='error'>" + s + "</p>";
		return r;
	}

	public static void googleLogin(DAO dao, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		if (user != null) {
			log.log(Level.WARNING, "googleLogin:"+user.getEmail()+":"+user.getNickname());
			Pessoa u = dao.procuraPessoa(user.getEmail());
			if (u != null) {
				log.log(Level.WARNING, "pessoa:"+u.getEmail()+":"+u.getNome());
				Sessao s = Sessao.init(req);
				s.set("loginName", u.getNome());
				s.set("loginEmail", u.getEmail());
				s.set("loginOk", "T");
				String r = s.get("loginRedirect");
				if (r == null)
					r = "inicio.page";
				s.set("loginRedirect", null);
				resp.sendRedirect(r);
			} else{
				log.log(Level.WARNING, "pessoa nova");
				resp.sendRedirect("/site/cadastro.page?email="
						+ user.getEmail());
			}
		} else {
			String url = userService.createLoginURL(req.getRequestURI());
			log.log(Level.WARNING, "logar em:"+url);
			resp.sendRedirect(url);
		}
	}
}